package utility;

public class Replace
{
  public static String Replace(String str,String replacestring)
  {
	int [] specialChar = {32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,58,59,60,61,62,63,64,91,92,93,94,95,96,123,124,125,126};
	int index=0;
	if(str.indexOf(replacestring)==-1)
		return str;
	else
	{
		while(index!=-1)
		{
			int indexofspchar =Index.indexOf(str, replacestring,index+1);
			if(indexofspchar!=-1)
			{
				boolean flag=false;
				try {
					char chara[] = str.toCharArray();
					for(int i=index;i<specialChar.length;i++)
					{
						if(indexofspchar+replacestring.length()<=chara.length-1)
							if(specialChar[i]==chara[indexofspchar+replacestring.length()])
							{
								flag = true;
								break;
							}
						if(specialChar[i]==chara[indexofspchar-1])
						{
							flag = true;
							break;
						}
					}
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				} 
				if(!flag)
				{
					//System.out.println(str.substring(0, indexofspchar));
					//System.out.println(str.substring(indexofspchar+replacestring.length(), str.length()));
					str =str.substring(0, indexofspchar) + str.substring(indexofspchar+replacestring.length(), str.length());
				}	
			}
			index = indexofspchar;
		}
	}
	return str;
  }
  
  public static String Replace(String str,String replacestring,String value)
  {
	int [] specialChar = {32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,58,59,60,61,62,63,64,91,92,93,94,95,96,123,124,125,126};
	int index=0;
	if(str.indexOf(replacestring)==-1)
		return str;
	else
	{
		while(index!=-1)
		{
			int indexofspchar =Index.indexOf(str, replacestring,index+1);
			if(indexofspchar!=-1)
			{
				boolean flag=false;
				try {
					char chara[] = str.toCharArray();
					for(int i=index;i<specialChar.length;i++)
					{
						if(indexofspchar+replacestring.length()<=chara.length-1)
							if(specialChar[i]==chara[indexofspchar+replacestring.length()])
							{
								flag = true;
								break;
							}
						if(specialChar[i]==chara[indexofspchar-1])
						{
							flag = true;
							break;
						}
					}
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				} 
				if(!flag)
				{
					//System.out.println(str.substring(0, indexofspchar));
					//System.out.println(str.substring(indexofspchar+replacestring.length(), str.length()));
					str =str.substring(0, indexofspchar) + value+str.substring(indexofspchar+replacestring.length(), str.length());
				}	
			}
			index = indexofspchar;
		}
	}
	return str;
  }
  
  public static void main(String[] args) 
  {
	 System.out.println(Replace("eee$$$e$$r$$o$$", "$$$","aaa"));
  } 
  
}
